home *** CD-ROM | disk | FTP | other *** search
- unit piegrpe;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
- Dialogs, PieGraf, DsgnIntF, TypInfo, StdCtrls, Mask, ColorGrd;
-
- type
-
- TPieGraphEditor = class(TForm)
- Label1: TLabel;
- ValuesListBox: TListBox;
- Label2: TLabel;
- AddBtn: TButton;
- ColorGrid1: TColorGrid;
- Label3: TLabel;
- RemoveBtn: TButton;
- OkBtn: TButton;
- CancelBtn: TButton;
- NewValue: TEdit;
- procedure FormCreate(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure ValuesListBoxDrawItem(Control: TWinControl; Index: Integer;
- Rect: TRect; State: TOwnerDrawState);
- procedure AddBtnClick(Sender: TObject);
- procedure RemoveBtnClick(Sender: TObject);
- procedure CancelBtnClick(Sender: TObject);
- procedure NewValueKeyPress(Sender: TObject; var Key: Char);
- private
- FPiePieces: TPiePieces;
- FPieGraphic: TPieGraphic; // Used as a Backup
- Modified: Boolean;
- procedure UpdateValuesListBox;
- end;
-
- { Now declare the TPropertyEditor descendant and override the
- required methods }
- TPiePiecesProperty = class(TPropertyEditor)
- function GetAttributes: TPropertyAttributes; override;
- function GetValue: String ; override;
- procedure Edit; override;
- end;
-
- { This function will be called by the property editor's Edit method. }
- function EditPiePieces(PiePieces: TPiePieces): Boolean;
-
- var
- PieGraphEditor: TPieGraphEditor;
-
- implementation
- {$R *.DFM}
-
- function IsCharNumeric(C: Char): Boolean;
- { This helper function determines whether or not a character is numeric }
- var
- Code, V: Integer;
- begin
- Val(C, V, Code);
- Result := Code = 0;
- end;
-
- function EditPiePieces(PiePieces: TPiePieces): Boolean;
- { Instanciates the TPieGraphEditor dialog which directly modifies
- the TPiePieces collection. }
- begin
- with TPieGraphEditor.Create(Application) do begin
- try
- FPiePieces := PiePieces; // Point to the actual TPiePieces collection
- { Copy the TPiePieces to the backup FPieGraphic which will be
- used as a backup in case the user cancels }
- FPieGraphic.PiePieces.Assign(PiePieces);
- { Draw the listbox with the list of TPiePieces Values. }
- UpdateValuesListBox;
- ShowModal; // Display the form.
- Result := Modified;
- finally
- Free;
- end;
- end;
- end;
-
- { TPieGraphEditor }
-
- procedure TPieGraphEditor.UpdateValuesListBox;
- { Updates the Values list box with updated information from the FPiePieces
- instance. The listbox stores the TPiePieces color in it's Objects array
- and then uses this in it's OnDrawItem event handle to draw the text in the
- specified color. }
- var
- i: Integer;
- begin
- ValuesListBox.Clear; // First clear the list box.
- for i := 0 to FPiePieces.Count - 1 do
- with FPiePieces[i] do
- ValuesListBox.Items.AddObject(IntToStr(WedgeValue), Pointer(Color));
- end;
-
- procedure TPieGraphEditor.FormCreate(Sender: TObject);
- { The OnCreate event handler instanciates a TPiePieces instance to hold a
- backkup of the original TPieWedge values. This allows the user to cancel
- any edits made. }
- begin
- FPieGraphic := TPieGraphic.Create(self);
- end;
-
- procedure TPieGraphEditor.FormDestroy(Sender: TObject);
- { Free the TPieGraphic instance. }
- begin
- FPieGraphic.Free;
- end;
-
- procedure TPieGraphEditor.ValuesListBoxDrawItem(Control: TWinControl;
- Index: Integer; Rect: TRect; State: TOwnerDrawState);
- { Uses an owner-draw list box to draw the TPieWedge values in their specified
- color }
- begin
- with ValuesListBox do begin
- Canvas.FillRect(Rect);
- Canvas.Font.Color := TColor(Items.Objects[Index]);
- DrawText(Canvas.Handle, PChar(Items[Index]),
- Length(Items[Index]), Rect, dt_Left or dt_VCenter);
- end;
- end;
-
- procedure TPieGraphEditor.AddBtnClick(Sender: TObject);
- { Adds a new TPieWedge to the collection and reflects the new information
- in the ValuesListBox }
- var
- PieWedge: TPieWedge;
- begin
- If StrToInt(NewValue.Text) > 0 then begin
- ValuesListBox.Items.Add(NewValue.Text);
- ValuesListBox.Refresh;
- PieWedge := FPiePieces.AddPiece(StrToInt(NewValue.Text),
- ColorGrid1.ForegroundColor);
- Modified := True;
- end;
- end;
-
- procedure TPieGraphEditor.RemoveBtnClick(Sender: TObject);
- { Removes the selected TPieWedge from the collection }
- var
- i: integer;
- begin
- i := ValuesListBox.ItemIndex;
- if i >= 0 then begin
- ValuesListBox.Items.Delete(i); // Remove the item from the listbox
- FPiePieces[i].Free; // Remove the item from the collection
- Modified := True;
- end;
- end;
-
- procedure TPieGraphEditor.CancelBtnClick(Sender: TObject);
- { Cancels any edits made to the collection. Copies the backup of the original
- TPiePieces collection back to the TPiePieces property. }
- begin
- FPiePieces.Assign(FPieGraphic.PiePieces);
- Modified := False;
- ModalResult := mrCancel;
- end;
-
- procedure TPieGraphEditor.NewValueKeyPress(Sender: TObject; var Key: Char);
- { This method ensures that any characters entered into the TEdit control are
- numeric only. }
- begin
- if not IsCharNumeric(Key) then
- Key := #0;
- end;
-
- { TPiePiecesProperty }
-
- function TPiePiecesProperty.GetAttributes: TPropertyAttributes;
- { Tell the Object Inspector that the property editor will use a
- dialog. This will cause the Edit method to be invoked when the user
- clicks the elipsis button in the Object Inspector. }
- begin
- Result := [paDialog];
- end;
-
- procedure TPiePiecesProperty.Edit;
- { Invoke the EditPiePieces() method and pass in the reference to the
- TPiePieces's instance being edited. This reference can be obtain by
- using the GetOrdValue method. Then redraw the PieGraphic by calling
- the TRunButtons.UpdatePiePieces method. }
- begin
- if EditPiePieces(TPiePieces(GetOrdValue)) then begin
- Modified;
- end;
- TPiePieces(GetOrdValue).UpdatePiePieces;
- end;
-
- function TPiePiecesProperty.GetValue: String;
- { Override the GetValue method so that the class type of the property
- being edited is displayed in the Object Inspector. }
- begin
- Result := Format('(%s)', [GetPropType^.Name]);
- end;
-
- end.
-